<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require_once 'resources/org/code/Code.class.php';

class LoginController extends CommonController
{
    //登录功能
    public function login(){

        if($input=Input::all()){
            $code = new \Code;
            $_code = $code->get();
            if($_code!=strtoupper($input['code'])){
                return back()->with('msg',"验证码错误");
            }
            $user = User::where('username',$input['username'])->first();
            if($user==null){
                return back()->with('msg',"用户名不存在");
            }
            if(Crypt::decrypt($user->password)!=$input['password']){
                return back()->with('msg',"密码不正确");
            }
            
            
            session(['user'=>['username'=>$user->username,'uid'=>$user->id]]);
//            dd(session('user'));
            return redirect('admin/index');
            
        }
        else{
            return view('admin.login');
        }
        
    }

    //验证码
    public function code(){
        $code = new \Code;
        $code->make();
    }
    //退出
    public function out()
    {
        session(['user'=>null]);
        echo "<script>parent.location.href='".url('admin/login')."'</script>";
    }
    
    
    
}
